package com.kaili.zuul.service.impl;

import com.kaili.common.entity.BaseMenu;
import com.kaili.common.entity.BaseUser;
import com.kaili.common.entity.LoginDetail;
import com.kaili.common.tenant.DbContextHolder;
import com.kaili.common.util.CommonConstant;
import com.kaili.common.util.CommonUtil;
import com.kaili.zuul.mapper.LoginMapper;
import com.kaili.zuul.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapper loginMapper;

    public LoginDetail loadUserByUsername(String s) throws UsernameNotFoundException {
        switchReadDb();

        return loginMapper.loadUserByUsername(s);
    }

    @Override
    public LoginDetail loadEmployeeByUsername(String s) throws UsernameNotFoundException {
        switchReadDb();

        return loginMapper.loadEmployeeByUsername(s);
    }

    @Override
    public List<BaseMenu> getAllBaseMenu() {
        switchReadDb();

        return loginMapper.getAllBaseMenu();
    }

    @Override
    public Map getSiteByEmployeeId(Integer employeeId) {
        switchReadDb();

        return loginMapper.getSiteByEmployeeId(employeeId);
    }

    @Override
    public String getCompanyName(Integer companyCode) {
        String currentDbType = DbContextHolder.getDbType();
        DbContextHolder.setDbType(CommonConstant.MAIN_DB_READ);
        String companyName = loginMapper.getCompanyName(companyCode);
        DbContextHolder.setDbType(currentDbType);

        return companyName;
    }

    @Override
    public String getParamValue(String paramKey) {
        return loginMapper.getParamValue(paramKey);
    }

    @Override
    @Cacheable(value = "verifyCode")
    public String getVerifyCode(String phone) {
        return "";
    }

    /**
     * 系统登陆成功后,根据当前语言环境,设置数据字典名称
     * @param language
     */
    @Override
    public void updateDict(){
        switchWriteDb();

        loginMapper.updateDict(CommonUtil.getLanguage());
    }
    /**
     * 主动切换数据源,因为AOP拦截service层,概率出现问题.
     */
    private void switchReadDb(){
        String type = "read";

        String companyId = CommonUtil.getCompanyId();
        DbContextHolder.setDbType(companyId+"_"+type);
    }

    private void switchWriteDb(){
        String type = "write";

        String companyId = CommonUtil.getCompanyId();
        DbContextHolder.setDbType(companyId+"_"+type);
    }

    @Override
    @CachePut(value = "translate")
    public Integer saveTranslate(String username, Integer translate) {
        return translate;
    }

    @Override
    @Cacheable(value = "translate")
    public Integer getTranslate(String username) {
        return null;
    }

    @Override
    public void updateUnlock(String name) {
        switchWriteDb();
        loginMapper.updateUnlock(name);
    }

    @Override
    public String systemParmeterConfig(String name) {
        switchWriteDb();
        return loginMapper.systemParmeterConfig(name);
    }

    //根据用户ID查询用户信息
    public BaseUser loadUserByUserid(Integer userId){
        return loginMapper.loadUserByUserid(userId);
    }


    //登录成功修改登录次数
    public int updateLoadTimes(Long id){
        return loginMapper.updateLoadTimes(id);
    }
}
